from django.db import models
from django.utils import timezone


# Create your models here.
# 用户
class UserModel(models.Model):
    nickname = models.CharField(max_length=64, verbose_name="用户昵称", default="点击设置昵称")
    openid = models.CharField(max_length=128, verbose_name='openid')
    avatar = models.ImageField(upload_to='avatar', verbose_name='用户头像', default='avatar/default.jpg')
    introduct = models.CharField(max_length=128, verbose_name='用户简介', default="我就是我，不一样的烟火~~")
    type = models.BooleanField(verbose_name='用户类型', default=False)
    grade = models.PositiveSmallIntegerField(verbose_name='用户年级', default=2024)
    collage = models.CharField(max_length=128, verbose_name='学院', default="默认学院")
    fans = models.PositiveIntegerField(verbose_name='粉丝数', default=0)
    attentions = models.PositiveIntegerField(verbose_name='关注数', default=0)

    def __str__(self):
        return self.nickname

    class Meta:
        verbose_name = "用户信息"
        verbose_name_plural = verbose_name


# 帖子
class PostModel(models.Model):
    author = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='作者')
    content = models.TextField(verbose_name='文章内容')
    pub_time = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
    likes = models.PositiveIntegerField(verbose_name='点赞数', default=0)
    type = models.IntegerField(verbose_name='类型', default=0)

    def __str__(self):
        return self.content

    class Meta:
        verbose_name = "文章"
        verbose_name_plural = verbose_name


# 评论
class CommentModel(models.Model):
    post = models.ForeignKey(PostModel, on_delete=models.CASCADE, verbose_name='文章')
    author = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='作者')
    content = models.TextField(verbose_name='评论内容')
    pub_time = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')

    def __str__(self):
        return self.content

    class Meta:
        verbose_name = "评论"
        verbose_name_plural = verbose_name


# 点赞动作
class LikeModel(models.Model):
    post = models.ForeignKey(PostModel, on_delete=models.CASCADE, verbose_name='文章')
    author = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='点赞人')
    time = models.DateTimeField(auto_now_add=True, verbose_name='点赞时间')

    def __str__(self):
        return self.post

    class Meta:
        verbose_name = "点赞"
        verbose_name_plural = verbose_name


# 关注
class FollowModel(models.Model):
    author = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='发起者', related_name='followers')
    follower = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='关注者', related_name='following')
    time = models.DateTimeField(auto_now_add=True, verbose_name='关注时间')

    def __str__(self):
        return self.author

    class Meta:
        verbose_name = "关注"
        verbose_name_plural = verbose_name


# 商品信息
class ShopModel(models.Model):
    name = models.CharField(max_length=128, verbose_name='商品名称')
    content = models.TextField(verbose_name='商品介绍', default='')
    price = models.FloatField(verbose_name='商品价格')
    picture = models.ImageField(upload_to='mainshop', verbose_name='商品图片')
    author = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='商品作者')
    time = models.DateTimeField(default=timezone.now, verbose_name='商品发布时间')
    status = models.BooleanField(verbose_name='商品状态', default=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "二手商品"
        verbose_name_plural = verbose_name


# 帖子图片
class PostPictureModel(models.Model):
    post = models.ForeignKey(PostModel, on_delete=models.CASCADE, verbose_name='文章')
    picture = models.ImageField(upload_to='postpicture', verbose_name='文章图片')

    def __str__(self):
        return self.picture

    class Meta:
        verbose_name = "文章图片"
        verbose_name_plural = verbose_name


# 订单
class OrderModel(models.Model):
    shop = models.ForeignKey(ShopModel, on_delete=models.CASCADE, verbose_name='商品')
    author = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='用户')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='价格')
    time = models.DateTimeField(auto_now_add=True, verbose_name='下单时间')
    status = models.BooleanField(verbose_name='状态', default=False)

    class Meta:
        verbose_name = "订单"
        verbose_name_plural = verbose_name
        unique_together = ('shop', 'author')


# 首页轮播图
class BannerModel(models.Model):
    title = models.CharField(max_length=128, verbose_name='标题')
    picture = models.ImageField(upload_to='banner', verbose_name='轮播图')
    content = models.TextField(verbose_name='内容')
    time = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "轮播图"
        verbose_name_plural = verbose_name


# 商家信息
class ShopInfoModel(models.Model):
    name = models.CharField(max_length=128, verbose_name='店铺名称')
    content = models.TextField(verbose_name='店铺介绍', default='')
    picture = models.ImageField(upload_to='shopinfo', verbose_name='店铺图片')
    position = models.CharField(max_length=128, verbose_name='店铺位置')
    phone = models.CharField(max_length=128, verbose_name='联系方式')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "店铺信息"
        verbose_name_plural = verbose_name


# 圈子
class CircleModel(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=128, verbose_name='圈子名称')
    content = models.TextField(verbose_name='圈子介绍', default='')
    picture = models.ImageField(upload_to='circle', verbose_name='圈子图片')
    author = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='圈子作者')
    time = models.DateTimeField(auto_now_add=True, verbose_name='圈子创建时间')
    number = models.PositiveIntegerField(verbose_name='圈子人数', default=1)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "圈子"
        verbose_name_plural = verbose_name


# 圈子成员
class CircleMemberModel(models.Model):
    circle = models.ForeignKey(CircleModel, on_delete=models.CASCADE, verbose_name='圈子')
    member = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='成员')
    time = models.DateTimeField(auto_now_add=True, verbose_name='加入时间')
    # 圈子成员+1
    def save(self, *args, **kwargs):
        circle = CircleModel.objects.get(pk=self.circle.id)
        circle.number += 1
        circle.save()
        super(CircleMemberModel, self).save(*args, **kwargs)

    # 圈子成员-1
    def delete(self, *args, **kwargs):
        circle = CircleModel.objects.get(pk=self.circle.id)
        circle.number -= 1
        circle.save()
        super(CircleMemberModel, self).delete(*args, **kwargs)
    def __str__(self):
        return self.circle.name

    class Meta:
        verbose_name = "圈子成员"
        verbose_name_plural = verbose_name


# 圈子帖子
class CirclePostModel(models.Model):
    circle = models.ForeignKey(CircleModel, on_delete=models.CASCADE, verbose_name='圈子')
    post = models.ForeignKey(PostModel, on_delete=models.CASCADE, verbose_name='帖子')

    def __str__(self):
        return self.circle.name

    class Meta:
        verbose_name = "圈子帖子"
        verbose_name_plural = verbose_name


# 圈子申请表
class CircleApplyModel(models.Model):
    circle = models.ForeignKey(CircleModel, on_delete=models.CASCADE, verbose_name='圈子')
    author = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='申请人')
    time = models.DateTimeField(auto_now_add=True, verbose_name='申请时间')
    status = models.BooleanField(verbose_name='状态', default=False)
    def __str__(self):
        return self.circle.name
    class Meta:
        verbose_name = "圈子申请表"
        verbose_name_plural = verbose_name